package com.apop.common.config;

import com.apop.common.log.LogInterceptor;
import com.apop.hrss.jwt.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * WebMvcConfig
 * Author: zjy
 * Date: 2021/11/9  15:23
 * Describe: WebMvc配置
 */
@Configuration
public class WebMvcConfig
        extends WebMvcConfigurationSupport
{

    // 日志拦截
    @Resource
    private LogInterceptor logInterceptor;

    @Resource
    private AppConfig configProperties;

    @Value("${res.image.path}")
    private String path;

    @Value("${res.image.h5path}")
    private String h5path;

    /**
     * 和页面有关的静态目录都放在项目的static目录下
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        //registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/static/**", "/favicon.ico").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/images/**").addResourceLocations("file:"+path);
        // 解决 SWAGGER 404报错
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        //上传的图片在D盘下的OTA目录下，访问路径如：http://localhost:8080/xxx.jpg
        super.addResourceHandlers(registry);
    }

    /**
     * 多个拦截器组成一个拦截器链
     *  addPathPatterns 用于添加拦截规则，/**表示拦截所有请求
     *  excludePathPatterns 用户排除拦截
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 是否开启请求日志拦截
        if(configProperties.getRequestLog()){
            registry.addInterceptor(logInterceptor).addPathPatterns("/**");
        }

        // 登陆拦截
        String[] patterns = new String[] {
                "/login",
                "/*.html",
                "/*.js",
                "/*.css",
                "/*.ico",
                "/*.png",
//                "/configuration/ui",
                "/configuration/ui",
                "/configuration/security",
                "/swagger-resources",
                "/swagger-resources/**",
                "/v2/api-docs",
                "/webjars/**",
//                "/webjars/bycdao-ui/images/api.ico",
                "/css/**",
                "/js/**",
                "/images/**",
                "/image/**",
                "/image/h5/**",
                "/layui/**",
                "/pz/**",
                "/pzH5/auth/h5login",
                "/pzH5/order/onPaySuccess",
                "/api/**",
                "/test/**",
                "/bank/**"

        };    // 添加不拦截的方法
        registry.addInterceptor(new JWTInterceptor())
                .addPathPatterns("/**")         // 其他接口token验证
                .excludePathPatterns(patterns);  // 不进行token验证

        super.addInterceptors(registry);
    }
}
